//
//  HELPrepareForExamVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELPrepareForExamVC.h"
#import "HELPrepareForExamCell.h"
#import "HELPrepareForExamHeaderView.h"
#import "HELPrepareForExamModel.h"
#import "NSString+Utilities.h"
#import "UIImageView+WebCache.h"
#import "HELWebViewController.h"
#import <UMMobClick/MobClick.h>
#import "HELSuperTableView.h"
static NSString *cellID = @"HELPrepareForExamCellID";

@interface HELPrepareForExamVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
@property(nonatomic, strong)HELPrepareForExamModel *prepareModel;
@property(nonatomic, copy)NSString *examId;
@end

@implementation HELPrepareForExamVC

- (instancetype)initWithExamId:(NSString *)examId {
    if (self = [super init]) {
        _examId = examId;
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"准备考试"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELPrepareForExamCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    [self addTableHeadView];
    [self footerAddBtnWith:@"开始考试"];
    [self requestData];
}


- (void)addTableHeadView {
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"HELPrepareForExamHeaderView" owner:nil options:nil];
    HELPrepareForExamHeaderView *tableHeadView = [nibContents lastObject];
    tableHeadView.bounds = CGRectMake(0, 0, kScreenWidth, 0.18*kScreenHeight);
    
    self.tableView.tableHeaderView = tableHeadView;
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
}



- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kAppBg;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}


- (void)footerAddBtnWith:(NSString *)title{
    
    UIButton *footerBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    footerBtn.frame =CGRectMake(15, kScreenHeight - 64 - 64, kScreenWidth -30, 40);
    [footerBtn setTitle:title forState:UIControlStateNormal];
    [footerBtn setBackgroundColor:[UIColor lightGrayColor]];
    footerBtn.layer.cornerRadius = 5;
    footerBtn.layer.masksToBounds= YES;
    footerBtn.enabled = NO;
    footerBtn.tag = 900;
    [footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerBtn addTarget:self action:@selector(beginToExam) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:footerBtn];
}
- (void)beginToExam {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    [MobClick event:@"StartExamCount" label:deviceName];
    HELWebViewController *webVC = [[HELWebViewController alloc] init];
    NSString *url = [[[HELNetworkManager sharedMg] getBaseUrl] stringByAppendingString:helBeginExamUrl];
    url = [url stringByAppendingFormat:@"?examId=%@&userId=%@",self.prepareModel.id,userInfo.userId];
    webVC.url = [NSURL URLWithString:url];
   [self.navigationController pushViewController:webVC animated:YES];
    
}

- (void)requestData {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    cwqHud(self.view);
    NSDictionary *param = @{@"id":self.examId,@"userId":userInfo.userId};
    
    defineWeakSelf;
    [[HELNetworkManager sharedMg] POST:helPrepareForExamUrl parameters:param success:^(id responseObject) {
        if (responseObject[@"exception"]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            HELPrepareForExamModel *model = [HELPrepareForExamModel mj_objectWithKeyValues:responseObject];
                weakSelf.prepareModel = model;
            HELPrepareForExamHeaderView *tableHeadView = (HELPrepareForExamHeaderView *)weakSelf.tableView.tableHeaderView;
            tableHeadView.title.text = model.title;
            [tableHeadView.bgImageView sd_setImageWithURL:[NSURL URLWithString:model.coverUrlShowMap.mid] placeholderImage:[UIImage imageNamed:@"暂无图片.jpg"]];
            UIButton *footBtn = [weakSelf.view viewWithTag:900];
            if ([model.examSign boolValue]) {
                footBtn.enabled = YES;
                [footBtn setBackgroundColor:HexRGB_WSJ(240, 88, 72)];
            }
            cwqHudSuccess(hud);
        }
        [weakSelf.tableView reloadData];
    } failure:^(NSString *err) {
        cwqHudRequestError(err);

    }];
}

#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 110;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELPrepareForExamCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    HELPrepareForExamModel *model = self.prepareModel;
    switch (indexPath.row) {
        case 0:
        {
            cell.title.text = @"考试内容";
            [cell.leftIcon setImage:[UIImage imageNamed:@"考试内容"]];
            cell.row0Line0.text = @"考题数量";
            if ([model.examNum integerValue] > 0) {
                cell.row0Line1.text = [NSString stringWithFormat:@"%@题",model.questionNum];
            }
            if ([model.examSecondShow integerValue] > 0) {
                
                cell.row1Line1.text = [NSString stringWithFormat:@"%@分钟",model.examSecondShow];
            }
            cell.row1Line0.text = @"考试时间";
        
        }
            break;
        case 1:
        {
            cell.title.text = @"合格标准";
            [cell.leftIcon setImage:[UIImage imageNamed:@"合格标准"]];
            cell.row0Line0.text = [NSString stringWithFormat:@"%@分满分", model.fullScore];
            cell.row0Line1.text = @"";
            cell.row1Line0.text = [NSString stringWithFormat:@"%@分及格", model.passScore];
            cell.row1Line1.text = @"";
            
        }
            break;

        case 2:
        {
            cell.title.text = @"开放时间";
            [cell.leftIcon setImage:[UIImage imageNamed:@"开放时间"]];
            cell.row0Line0.text = @"开始时间";
            cell.row0Line1.text = model.beginOpenTimeShow;
            cell.row1Line0.text = @"结束时间";
            cell.row1Line1.text = model.endOpenTimeShow;
            
        }
            break;

            
            
            
        default:
            break;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
